package com.example.blogsystem.advice;

import com.example.blogsystem.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Configuration
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String jwt = request.getHeader("user_token");
        log.info("从请求中获取Header: " + jwt);
        Claims claims = JwtUtils.parseJwt(jwt);
        if(claims != null) {
            log.info("用户校验成功");
            return true;
        }
        response.setStatus(401);//未登录
        return false;
    }
}
